package com.sword.service.impl.stock.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.pojo.finance.ReceivablesPay;
import com.sword.pojo.stock.product.Product;
import com.sword.service.impl.BaseServiceImpl;
import com.sword.service.stock.product.ProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sword.service.impl.BasicBaseServiceImpl;

import java.lang.reflect.Field;
import java.util.Date;

@Service
@Transactional
public class ProductServiceImpl extends BasicBaseServiceImpl<Product> implements ProductService {


    @Override
    public PageResult queryProduct(QueryPageBean queryPageBean) {

        QueryWrapper<Product> wrapper =new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        // 如果queryString有值
        if (queryString != null) {
            Product product = new Product();
            Field[] fields = product.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                //有的字段是用private修饰的 将他设置为可读
                fields[i].setAccessible(true);
                if(fields[i].getName().endsWith("Date")){
                    continue;
                }
                wrapper.like(fields[i].getName(),queryString).or();
            }
        }
        IPage<Product> ipage = super.queryPageList(wrapper, queryPageBean.getPage(), queryPageBean.getLimit());

        return new PageResult(0, MessageConstant.QUERY_PRODOCT_SUCCESS, ipage.getTotal(), ipage.getRecords());
    }


    @Override
    public Integer addProduct(Product product) {
        product.setCreateDate(new Date());
        return super.save(product); }

    @Override
    public Integer editProduct(Product product) {
        return super.update(product);
    }

    @Override
    public Integer deleteProduct(Integer id) {
        return super.deleteById(Long.valueOf(id));
    }

}
